package live.ifox.stage;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import live.ifox.base.BaseRestActivity;
import live.ifox.factory.Entry;
import live.ifox.factory.ID;
import live.ifox.factory.Url;
import live.ifox.helper.CommonHelper;

import java.util.HashMap;
import java.util.Map;

public class ContactEditor extends BaseRestActivity {
    boolean updateFlag;
    Button btn_submit;
    EditText et_province, et_city;
    EditText et_name, et_phone, et_remark;
    int _id;
    String _province, _city;
    String _name, _phone, _remark;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact_editor);

        updateFlag = false;
        entry = Entry.MedicalContact;
        baseUrl = Url.MedicalContact;
        btn_submit = findViewById(R.id.submit);

        et_province = findViewById(R.id.province);
        et_city = findViewById(R.id.city);
        et_name = findViewById(R.id.name);
        et_phone = findViewById(R.id.phone);
        et_remark = findViewById(R.id.remark);

        Intent intent = getIntent();
        if (intent == null)
            _id = 0;
        else
            _id = intent.getIntExtra("id", 0);

        if (_id > 0) {
            updateFlag = true;
            btn_submit.setText(R.string.save);

            _province = intent.getStringExtra("province");
            _city = intent.getStringExtra("city");
            _name = intent.getStringExtra("name");
            _phone = intent.getStringExtra("phone");
            _remark = intent.getStringExtra("remark");

            et_province.setText(_province);
            et_city.setText(_city);
            et_name.setText(_name);
            et_phone.setText(_phone);
            et_remark.setText(_remark);
        }
    }

    public void onCancel_Click(View view) {
        finish();
    }

    public void onSubmit_Click(View view) {
        String province = et_province.getText().toString();
        if (province.isEmpty())
            province = "中国";

        String city = et_city.getText().toString();
        if (city.isEmpty())
            city = "未知";

        String name = et_name.getText().toString();
        String phone = et_phone.getText().toString();
        if (name.isEmpty() || phone.isEmpty()) {
            CommonHelper.showMessage(this, "姓名、手机号不能为空！");
            return;
        }

        String remark = et_remark.getText().toString();
        Map<String, Object> map = new HashMap<>();
        if (!updateFlag) {
            map.put("province", province);
            map.put("city", city);
            map.put("name", name);
            map.put("phone", phone);
            map.put("remark", remark);
            httpPost(baseUrl + "insert", JSON.toJSONString(map));
        } else {
            if (!province.equals(_province))
                map.put("province", province);

            if (!city.equals(_city))
                map.put("city", city);

            if (!name.equals(_name))
                map.put("name", name);

            if (!phone.equals(_phone))
                map.put("phone", phone);

            if (!remark.equals(_remark))
                map.put("remark", remark);

            if (map.isEmpty())
                return;

            map.put("id", _id);
            httpPost(baseUrl + "update", JSON.toJSONString(map));
        }
        setResult(ID.RESULT_DONE);
        finish();
    }

    @Override
    public void processReply(JSONObject rootObject) {
    }
}
